package com.ctshk.rpc.order.custom.provider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.order.custom.req.order.CustomOrderReceiptListTouristReq;
import com.ctshk.rpc.order.custom.req.order.OrderReceiptListReq;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

/**
 * 订单收据provider
 */
public class CustomOrderReceiptProvider {

    public String selectListBySelective(@Param("page") IPage<OrderReceiptListReq> page, @Param("req") OrderReceiptListReq req) {
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("tsor.id, tsor.order_id, tsor.schedule_id, ");
        sb.append("tsor.receipt_number, tsor.receipt_header, tsor.receipt_type, tsor.receipt_amount, ");
        sb.append("tsor.include_tourist_num, tsor.create_time, tsor.void_time, tsor.void_reason, ");
        sb.append("tsor.receipt_status ");
        sb.append("FROM t_customized_order_receipt tsor ");
        sb.append("where tsor.is_deleted = 0 ");
        sb.append("and tsor.order_id = #{req.orderId} ");
        if (StringUtils.isNotBlank(req.getReceiptNumber())) {
            sb.append("and tsor.receipt_number=#{req.receiptNumber} ");
        }
        if (req.getReceiptType() != null) {
            sb.append("and tsor.receipt_type=#{req.receiptType} ");
        }
        if (req.getReceiptStatus() != null) {
            sb.append("and tsor.receipt_status=#{req.receiptStatus} ");
        }
        return sb.toString();
    }


    public String selectListTouristBySelective(IPage<CustomOrderReceiptListTouristReq> pageParam, @Param("req") CustomOrderReceiptListTouristReq req)  {
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("tsoti.id, tsoti.order_id, tsoti.schedule_id, ");
        sb.append("tsoti.cn_surname, tsoti.cn_name, ");
        sb.append("tsoti.receipted_amount, (tsoti.total_paid_in - tsoti.receipted_amount) AS not_received_amount ");
        sb.append("FROM t_customized_order_tourist_info tsoti ");
        sb.append("where tsoti.is_deleted = 0 AND tsoti.is_full_info = 1 ");
        sb.append("and tsoti.order_id = #{req.orderId}");
        return sb.toString();
    }


    public String selectCountListTouristBySelective(@Param("req") CustomOrderReceiptListTouristReq req)  {
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("sum(tsoti.total_paid_in - tsoti.receipted_amount) AS not_received_amount ");
        sb.append("FROM t_customized_order_tourist_info tsoti ");
        sb.append("where tsoti.is_deleted = 0 ");
        sb.append("and tsoti.order_id = #{req.orderId} AND tsoti.is_deleted = 0 AND tsoti.is_full_info = 1");
        return sb.toString();
    }

}
